import ipaddress
import platform
import subprocess
import threading


def ping (ips):
    result = subprocess.call("ping %s" % ips)
    if result == 0:
        with open('hw2_2.txt', 'a') as fp:
         fp.write(f"网段{ip}中{ips}已经被使用\n")
         print("成功")
    else:
        with open('hw2_2.txt', 'a') as fp:
         fp.write(f"网段{ip}中{ips}未被使用\n")
         print("失败")



if __name__ == '__main__':

 ip = input("请输入你期望检查的ip网段：")
 test = input("请输位数子网掩码..中的内容--/..格式")
 net = ipaddress.ip_network(f'{ip}.0/{test}')
 print(net.num_addresses)
 file = open("test.txt", 'w+').close()
 for ips in net:
     t = threading.Thread(target=ping, args=(ips,))
     t.start()




